package com.jxbeidou.system.org.controller;

import lombok.AllArgsConstructor;
import com.jxbeidou.core.entity.PageResult;
import com.jxbeidou.core.entity.R;
import com.jxbeidou.system.org.convert.OrgConvert;
import com.jxbeidou.system.org.entity.OrgDO;
import com.jxbeidou.system.org.service.OrgService;
import com.jxbeidou.system.org.query.OrgQuery;
import com.jxbeidou.system.org.vo.OrgVO;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import jakarta.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
* 机构管理
*
* @author 杜晓祥 
* @since 1.0.0 2023-12-14
*/
@RestController
@RequestMapping("/sys/org")
@AllArgsConstructor
public class OrgController {
    @Resource
    private OrgService orgService;

    @PostMapping("/page")
    public R<PageResult<OrgVO>> page(@RequestBody @Valid OrgQuery query){
        PageResult<OrgVO> page = orgService.page(query);

        return R.success(page);
    }

    @GetMapping("/{id}")
    public R<OrgVO> get(@PathVariable("id") Long id){
        OrgDO entity = orgService.getById(id);

        return R.success(OrgConvert.INSTANCE.convert(entity));
    }

    @PostMapping
    public R<String> save(@RequestBody OrgVO vo){
        orgService.save(vo);

        return R.saveSuccess();
    }

    @PutMapping
    public R<String> update(@RequestBody @Valid OrgVO vo){
        orgService.update(vo);

        return R.modifySuccess();
    }

    @DeleteMapping("/{id}")
    public R<String> delete(@PathVariable("id") Long id){
        orgService.delete(Arrays.asList(id));

        return R.deleteSuccess();
    }

    @PutMapping("/delete")
    public R<String> delete(@RequestBody List<Long> idList){
        orgService.delete(idList);

        return R.deleteSuccess();
    }
}